<template>
  <div>
    <el-card class="top"><el-header>商品兑换记录</el-header></el-card>

    <!-- 表格展示 -->
    <el-card>
      <el-input placeholder="商品名称" prefix-icon="el-icon-search" style="width: 200px" v-model="name">
      </el-input>
      <el-button type="warning" @click="reload" plain>查询</el-button>
      <el-table :data="goodsList" style="width: 100%" :sort-by.sync="sortKey"
        :sort-orders="['ascending', 'descending']">
        <el-table-column type="index" label="编号" width="50" align="center">
        </el-table-column>
        <el-table-column prop="uname" label="用户名" width="100" align="center">
        </el-table-column>
        <el-table-column prop="name" label="真实姓名" width="100" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="联系方式" width="130" align="center">
        </el-table-column>
        <el-table-column prop="gname" label="商品名称" width="80" align="center">
        </el-table-column>
        <el-table-column prop="address" label="收获地址" width="150" align="center">
        </el-table-column>
        <el-table-column prop="num" label="兑换数量" width="100" align="center" sortable>
        </el-table-column>
        <el-table-column prop="totalCost" label="总消耗积分" width="120" align="center" sortable>
        </el-table-column>
        <el-table-column prop="time" label="兑换时间" width="170" align="center" sortable>
        </el-table-column>
        <el-table-column prop="state" label="状态" width="80" align="center" sortable>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" v-if="canEditAndDelete">
          <template slot-scope="scope">
            <!-- v-if="canEditAndDelete"  v-else-->
            <el-button size="mini" type="primary" @click="examine(scope.row)">审核</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--    分页-->
    <el-row>
      <el-col style="text-align:right">
        <el-pagination :current-page="currentPage" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-size="pageSize" :page-sizes="[5, 10, 15]" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </div>

</template>

<script>
import { del } from 'vue';

export default {
  data() {
    return {
      rolename: '',
      sortKey: '',
      name: "",
      goodsList: [],
      formLabelWidth: '120px',
      //配置分页
      currentPage: 1,
      pageSize: 5,
      total: 0
    };
  },

  created() {
    const storedUserInfo = localStorage.getItem('user');
    if (storedUserInfo) {
      this.user = JSON.parse(storedUserInfo);
      console.log(this.user);
      this.rolename = this.user.roleList[0].name;
      this.phone = this.user.phone;
      this.username = this.user.name;
      console.log(this.rolename);
    }
  },

  computed: {
    // 根据用户角色决定是否显示编辑和删除按钮
    canEditAndDelete() {
      //return this.rolename === 'admin';
      const rolenames = ['超级管理员', '产品经理', '后台管理员', '客服经理'];
      return rolenames.includes(this.rolename); // 只有指定角色可以编辑和删除
    }
  },

  mounted() {
    this.reload();
  },

  methods: {

    examine(row) {
      console.log(row);
      if (row.state == "已审核") {
        this.$message({
          message: "该商品已被审核，不可重复审核",
          type: 'error'
        });
      } else {
        this.$axios.put("/goods/editState/" + row.id).then(res => {
          if (res.data.code == 200) {
            this.$message({
              message: "审核成功",
              type: 'success'
            });
            this.buyformVisible = false
          } else {
            this.$message.console.error("审核失败");
          }
          this.reload();
        })
      }
    },

    reload() {
      this.$axios.get("/goods/all?currentPage=" + this.currentPage + "&pageSize=" + this.pageSize + "&name=" + this.name).then((res) => {
        console.log(res.data.data);
        this.goodsList = res.data.data.list;
        this.total = res.data.data.total;
        this.money = res.data.data.list.money;
      });
    },
    //显示查询出的总条数
    handleSizeChange(val) {
      this.pageSize = val;
      this.reload();
    },

    //点击新页查询的数据
    handleCurrentChange(val) {
      this.currentPage = val;
      this.reload();
    },
  },
};
</script>

<style scoped>
.el-header {
  text-align: center;
  line-height: 30px;
  size: 120px;
}

.top {
  background-color: #D3D3D3;
  height: 60px;
}

.el-card {
  margin-top: 10px;
  margin-left: 30px;
}

.el-table .warning-row {
  background: oldlace;
}

.el-table .success-row {
  background: #f0f9eb;
}
</style>